package com.zlm.user.partner.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.constants.ActivationContants;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.entity.SysActivationDO;
import com.zlm.common.util.DateTimeUtils;
import com.zlm.user.partner.dao.SysActivationDao;
import com.zlm.user.partner.service.SysActivationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 验证码服务实现类
 * @Author: duyufu
 * @Date: 2019/4/8 11:55
 */
@Service
public class SysActivationServiceImpl extends AbstractBaseService<SysActivationDao, SysActivationDO> implements SysActivationService {

    @Autowired
    @Override
    public void setBaseDao(SysActivationDao sysActivationDao) {
        this.baseDao = sysActivationDao;
    }

    @Override
    public String sendActivation(String mobile) {
        //查询今天一共发了多少条信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mobile", mobile);
        map.put("sysDate", DateTimeUtils.getCurrentDateString());
        Integer count = baseDao.getCountActivation(map);
        if(count == ActivationContants.MAX_COUNT){
            throw new RuntimeException(CodeConstants.CODE_10006);
        }
        //发送验证码
        String code = "";
        try {
//			code= SmsUtils.alisend(mobile);
            //测试专用
            code="111111";
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(CodeConstants.CODE_BUSY);
        }
        if (StringUtils.isBlank(code)) {
            throw new RuntimeException(CodeConstants.CODE_FAIL);
        }
        //将信息保存到数据库
        SysActivationDO activation = new SysActivationDO();
        activation.setType(ActivationContants.ACTIVATION_TYPE_MOBILE);
        activation.setCode(code);
        activation.setCreateUser("zhaolemo");
        activation.setMobile(mobile);
        activation.setStatus(ActivationContants.ACTIVATION_STATUS_DEFAULT);
        activation.setValidTime(DateTimeUtils.getTimeAddMinute(ActivationContants.VALID_TIME));
        baseDao.saveActivationCode(activation);
        return CodeConstants.CODE_SUCCESS;
    }

    @Override
    public SysActivationDO getActivation(SysActivationDO activation) {
        SysActivationDO activationDO = baseDao.getActivation(activation);
        return activationDO;
    }

    @Override
    public void changeStatus(Integer id) {
        baseDao.changeStatus(id);
    }

    @Override
    public String checkActivation(String activationCode, String mobile) {
        SysActivationDO activation = new SysActivationDO();
        activation.setMobile(mobile);
        SysActivationDO sysActivationDO = baseDao.getActivation(activation);
        if (null == sysActivationDO || !activationCode.equals(sysActivationDO.getCode())) {
            throw new RuntimeException(CodeConstants.CODE_10002);
        }
        //验证码是否失效
        Long currentTime = DateTimeUtils.getCurrentTimestamp().getTime();
        Long validTime = sysActivationDO.getValidTime().getTime();
        if (currentTime > validTime) {
            throw new RuntimeException(CodeConstants.CODE_10005);
        }
        baseDao.changeStatus(sysActivationDO.getId());
        return CodeConstants.CODE_SUCCESS;
    }

}
